if(!dojo._hasResource['vifito.galdic.widgets.GalipediaBox']){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource['vifito.galdic.widgets.GalipediaBox'] = true;
dojo.provide('vifito.galdic.widgets.GalipediaBox');

dojo.require('dijit._Widget');
dojo.require('dijit._Templated');

dojo.require("dijit.Dialog");
dojo.require('vifito.galdic.WSGalipedia');

dojo.declare(
    "vifito.galdic.widgets.GalipediaBox",
    
    [dijit._Widget, dijit._Templated],
    
    {
        templateString: '<div></div>',
        
        handleOpensearch: null,
        handleParse: null,
        
        dialogGl: null,
    
        postCreate: function() {
            handleOpensearch = dojo.subscribe('WSGalipedia.opensearch', 
                dojo.hitch(this, this._processSearch));
                
            handleParse = dojo.subscribe('WSGalipedia.parse', 
                dojo.hitch(this, this._processPage));
            
            dialogGl = new dijit.Dialog({
                title: "Galipedia",
                style: "width: 960px"
            });                
        }, 
        
        _processSearch: function(result) {
            if(result[1].length>0) { // Hay resultados
                var results = result[1];
                var output = '<h1>Resultados atopados na Galipedia</h1><ul class="galipedia-links">';
                
                for(var i=0; i<results.length; i++) {
                    output += '<li><a href="http://gl.wikipedia.org/wiki/' + 
                        encodeURIComponent(results[i]) + '">' + results[i] + '</li>';
                }
                
                this.domNode.innerHTML = output + '</ul>';
            } else {
                this.domNode.innerHTML = '';
            }
            
            dojo.forEach( dojo.query('ul li a', this.domNode),
                dojo.hitch(this, this._attachClickEvent)
            );
        },
        
        _processPage: function(result) {
            var content = result.parse.text['*'];
            content = content.replace(/ href=(["'])\//gi, 
                ' href=$1http://gl.wikipedia.org/');
        
            dialogGl.attr("content", content);
            dialogGl.show();  
            
            dojo.style(dialogGl.containerNode, {
                    width: '90%'
            });          
        },
        
        _attachClickEvent: function(node, i) {
            dojo.connect(node, 'onclick', dojo.hitch(this, this.showEntry));
        },
        
        clear: function() {
            this.domNode.innerHTML = '';
        },
        
        showEntry: function(evt) {
            dojo.stopEvent(evt);
            
            var wsg = new vifito.galdic.WSGalipedia();
            wsg.parse(evt.target.innerHTML);
        }, 
        
        destroy: function() {
            dojo.unsubscribe(handleOpensearch);
            this.inherited(arguments);
        }
    }
);


}
